package com.ht.web.aduit;

import com.ht.bean.aduit.Gemp;
import com.ht.bean.aduit.Notice;
import com.ht.bean.emp.Empinfo;
import com.ht.bean.json.JsonData;
import com.ht.service.aduit.GempService;
import com.ht.service.aduit.NoticeService;
import com.ht.service.education.TrialService;
import com.ht.util.Pager;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author chun
 * @date 2020/6/28 15:15
 */
@Controller
@RequestMapping("/notice")
public class NoticeController {

    private static Integer i=0;

    @Resource
    private NoticeService noticeService;

    @Resource
    private JsonData jsonData;

    @Resource
    private TrialService trialService;

    @Resource
    private GempService gempService;

    //去到公告页面
    @RequestMapping("/list")
    public String list(Map map){
        List<Empinfo> emplist = trialService.Emplist();//查询emp表
        map.put("elist",emplist);
        return "aduit/noticelist";
    }

    //公告数据
    @RequestMapping("/data")
    @ResponseBody
    public JsonData jsonData(@Param("limit") int limit, @Param("page")int page,@Param("notice")Notice notice){
        Pager pager = new Pager();
        pager.setCurrPage(page);
        pager.setPageSize(limit);
        jsonData.setCount(noticeService.selNot(notice));
        jsonData.setData(noticeService.selpage(pager,notice));
        return jsonData;
    }

    //公告新增
    @RequestMapping("/add")
    @ResponseBody
    public Integer add(HttpSession session, Notice notice){
        //System.out.println("公告添加数据"+notice);
        Empinfo user = (Empinfo) session.getAttribute("user");
        //session.invalidate();
        //员工姓名
        notice.setEmpid(user.getEmp_id());
        //发布时间
        notice.setGdate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        //已读人数
        notice.setGread(0);

        //新增时判断是学生还是员工
        if(notice.getGtype().equals("员工")){
            //System.out.println("11111111111");
            //设置员工未读人数
            notice.setGunread(noticeService.selEmp());
            //新增操作
            int insert = noticeService.insert(notice);
            return insert;
        }
        //设置学生未读人数
        notice.setGunread(noticeService.selStu());
        //新增操作
        int insert = noticeService.insert(notice);
        return insert;
    }

    @RequestMapping("/upd")
    @ResponseBody
    public Integer upd(Notice notice){
        int i = noticeService.updateByPrimaryKeySelective(notice);
        return i;
    }

    @RequestMapping("/toupd")
    public String toupd(Integer gid, Map map){
        Notice notice = noticeService.selectByPrimaryKey(gid);
        map.put("list",notice);
        return "aduit/noticeupd";
    }

    @RequestMapping("/toadd")
    public String toadd(){
        return "aduit/noticeadd";
    }

    @RequestMapping("/del")
    @ResponseBody
    public Integer del(Integer gid){
        int i = noticeService.deleteByPrimaryKey(gid);
        return i;
    }

    //查看
    @RequestMapping("/idtoupd")
    public String idtoupd(Integer gid, Map map, Notice notice1,HttpSession session){
        Empinfo empinfo=(Empinfo)session.getAttribute("user");
        Gemp gemp=new Gemp();
        gemp.setGid(gid);
        gemp.setEmp_id(empinfo.getEmp_id());

        if(gempService.bygidtotal(gid)==0){//查公告id
            gempService.insert(gemp);//新增方法
        }else {//判断该用户在当公告是否已读 0未读
            if(gempService.byGidEid(gemp)==0){//查读没
                gempService.insert(gemp);
            }
        }
        //传到前台
        Notice notice = noticeService.selectByPrimaryKey(gid);
        map.put("notice",notice);

        //设置已读  未读
        notice1.setGread(gempService.bygidtotal(gid));
        if(notice.getGtype().equals("员工")){
            notice1.setGunread(noticeService.selEmp()-gempService.bygidtotal(gid));
        }else {
            notice1.setGunread(noticeService.selStu()-gempService.bygidtotal(gid));
        }
        //修改已读未读
        noticeService.updateByPrimaryKeySelective(notice1);
        return "aduit/noticeidupd";
    }
}
